package com.wenhua.service.impl;

import com.wenhua.mapper.DormitoryMapper;
import com.wenhua.pojo.Dormitory;
import com.wenhua.pojo.DormitoryBuilding;
import com.wenhua.service.DormitoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DormitoryServiceImpl implements DormitoryService {

    @Autowired
    private DormitoryMapper dormitoryMapper;
    @Override
    public List<DormitoryBuilding> selectDormitoryBuilding(Integer currentPage, Integer pageSize, String selectString, String sort) {
        //首先计算偏移量
        Integer offset = (currentPage - 1) * pageSize;

        //调用mapper查询
        List<DormitoryBuilding> dormitoryBuildings = dormitoryMapper.selectDormitoryBuilding(offset, pageSize, selectString, sort);

        return dormitoryBuildings;
    }

    /* 查询宿舍楼数量 */
    @Override
    public Integer selectDormitoryBuildingCount(String selectString) {
        /* 调用mapper */
        Integer count = dormitoryMapper.selectDormitoryBuildingCount(selectString);
        return count;
    }

    /* 查询宿舍 */
    @Override
    public List<Dormitory> selectDormitory(Integer currentPage, Integer pageSize, String selectString, String sort) {
        /* 计算偏移量 */
        Integer offset = (currentPage - 1) * pageSize;
        /* 调用mapper */
        List<Dormitory> dormitoryList = dormitoryMapper.selectDormitory(offset, pageSize, selectString, sort);

        return dormitoryList;
    }

    /* 查询宿舍数量 */
    @Override
    public Integer selectDormitoryCount(String selectString) {
        /* 调用mapper */
        Integer count = dormitoryMapper.selectDormitoryCount(selectString);
        return count;

    }

    /* 查询宿舍楼 */
    @Override
    public List<DormitoryBuilding> selectDormitoryBuildingAll() {
        List<DormitoryBuilding> dormitoryBuildings = dormitoryMapper.selectDormitoryBuildingAll();
/*        dormitoryBuildings.forEach(d->{
            System.out.println(d.getActualDormitoryCount());
        });*/
        return dormitoryBuildings;
    }

    /* 添加宿舍楼 */
    @Override
    public Integer insertDormitory(Dormitory dormitory) {
        //先添加宿舍楼
        Integer i = dormitoryMapper.insertDormitory(dormitory);
        //再添加到宿舍-宿舍楼关系表
        Integer j = dormitoryMapper.insertDormitory2DormitoryBuilding(dormitory);
        return i+j>0?1:0;
    }

    /* 添加宿舍楼 */
    @Override
    public Integer insertDormitoryBuilding(DormitoryBuilding dormitoryBuilding) {
        Integer i = dormitoryMapper.insertDormitoryBuilding(dormitoryBuilding);
        return i>0?1:0;
    }

    /* 根据宿舍楼id查询宿舍 */
    @Override
    public List<Dormitory> selectDormitoryByBuildingId(Integer buildingId) {
        List<Dormitory> dormitoryList = dormitoryMapper.selectDormitoryByBuildingId(buildingId);
        return dormitoryList;
    }

    /* 更新宿舍 */
    @Override
    public Integer updateDormitory(Dormitory dormitory) {
        //先更新宿舍信息
        Integer i = dormitoryMapper.updateDormitory(dormitory);

        //再更新宿舍-宿舍楼关系表
        Integer j = dormitoryMapper.updateDormitory2DormitoryBuilding(dormitory.getBuildingId(),dormitory.getDormitoryId());
        return 0;
    }


    /* 更新宿舍楼 */
    @Override
    public Integer updateDormitoryBuilding(DormitoryBuilding dormitoryBuilding) {
        //
        Integer i = dormitoryMapper.updateDormitoryBuilding(dormitoryBuilding);
        return i>0?1:0;

    }

    /* 删除宿舍 */
    @Override
    public Integer deleteDormitory(Dormitory dormitory) {
        Integer i = dormitoryMapper.deleteDormitory(dormitory);
        return i>0?1:0;
    }

        /* 删除宿舍楼 *//**/
    @Override
    public Integer deleteDormitoryBuilding(DormitoryBuilding dormitoryBuilding) {
        Integer i = dormitoryMapper.deleteDormitoryBuilding(dormitoryBuilding);
        return i>0?1:0;
    }
}
